<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 20:37
 */

namespace app\admin\model;

class Icon extends \think\Model
{


    /**
     * 获取单条
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     */
    public function _findOne($id){
        return $this->where([['id', '=', $id]])->find();
    }


    

    /**
    * 获取所有菜单
    *
    */
    public function _getIconAll($where = []){
        $data = $this->where($where)->order('updated')->select()->toArray();
        return $data;
    }


    /**
     * 列表
     * @param array $condition
     * @param int $page_size
     * @param string $order_by
     * @return \think\Paginator
     */
    public function _list($condition = [], $page_size = PAGE_SIZE_ADMIN, $order_by = 'updated desc'){
        $obj = $this->order($order_by);
        $result = get_page($obj, $condition, $page_size);
       
        return $result;
    }


    /**
     * 添加
     * @param $post
     * @return bool
     */
    public function _create($post){
        $icon = trim($post['icon']);
        $data = $this->where([['icon', '=', $icon]])->find();
        if(!empty($data)){
            $this->error = '图标已存在';
            return false;
        }
        $insert = [
            'icon' => $icon,
            'updated' => time(),
            'created' => time(),
            
        ];
        if(!$this->save($insert)){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }

    /**
     * 修改
     * @param $post
     * @return bool
     */
    public function _modify($post){
        $role = $this->_findOne($post['id']);
        
        if(empty($role)){
            $this->error = '没有此图标';
            return false;
        }
        
        $icon = trim($post['icon']);
        $data = $this->where([['icon', '=', $icon]])->find();
        if($data->id != $role->id){
            $this->error = '图标已存在';
            return false;
        }
        
        $update = [
            'icon' => trim($post['icon']),
            'updated' => time(),
        ];
        $condition = [
            'id' => $post['id']
        ];
        $ret = $this->save($update, $condition);
        if($ret === false){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }


    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function _remove($id){
        $role = $this->_findOne($id);
        if(empty($role)){
            $this->error = '没有此图标';
            return false;
        }
        

        if(!$role->delete()){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }

    /**
    * 数据总条数
    */
    public function _count($where = []){
        return $this->where($where)->count();
    }

}